import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:template_flutter/page/home_page.dart';
import 'package:template_flutter/page/message_page.dart';
import 'package:template_flutter/page/my_page.dart';
import 'package:template_flutter/page/shop_page.dart';
import 'package:template_flutter/view_model/main_view_model.dart';

void main() {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    // Brightness.light 状态栏字体颜色（白色）
    statusBarIconBrightness: Brightness.dark, // 状态栏字体颜色（黑色）
    // statusBarColor: Colors.yellow, // 状态栏背景色
  ));
  //1，本地存储
  //2，国际化语言
  //3，路由
  //4，状态管理器
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: MainViewModel()),
        ],
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(),
        ));
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var page = const [HomePage(), MessagePage(), ShopPage(), MyPage()];

  final _skey = GlobalKey<ScaffoldState>();

  int currentIndex = 0;
  DateTime? _lastPressedAt; //上次点击时间
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 1)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          Fluttertoast.showToast(msg: "再次点击退出程序");
          return false;
        }
        return true;
      },
      child: Scaffold(
        key: _skey,
        body: page[currentIndex],
        bottomNavigationBar: BottomNavigationBar(
          showSelectedLabels: true,
          showUnselectedLabels: true,
          selectedFontSize: 12,
          unselectedFontSize: 12,
          enableFeedback: true,
          currentIndex: currentIndex,
          type: BottomNavigationBarType.fixed,
          items: const [
            BottomNavigationBarItem(
                backgroundColor: Colors.blue,
                icon: Icon(Icons.home),
                label: "首页"),
            BottomNavigationBarItem(
                backgroundColor: Colors.green,
                icon: Icon(Icons.message),
                label: "消息"),
            BottomNavigationBarItem(
                backgroundColor: Colors.amber,
                icon: Icon(Icons.shopping_cart),
                label: "购物车"),
            BottomNavigationBarItem(
                backgroundColor: Colors.red,
                icon: Icon(Icons.person),
                label: "我的"),
          ],
          onTap: (value) {
            setState(() {
              currentIndex = value;
            });
          },
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.ac_unit),
          onPressed: () {},
        ),
      ),
    );
  }
}
